package homework;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Objects;

public class ArrayListTest02 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("hello");
        list.add("world");
        list.add("java");
        list.add("asdadasd");
        list.add("你好哇");
        list.add("网不好");
        list.add("还行吧ok");
        System.out.println(list);

        //获取最大元素
        System.out.println(Collections.max(list));
        //获取最小元素
        System.out.println(Collections.min(list));
        //删除长度大于5的字符串
        list.removeIf(s -> s.length() >= 5);
        System.out.println(list);

        //反转集合中的元素,倒序
        for (int i = 0; i < list.size()/2; i++) {
            String s = list.get(i);
            list.set(i, list.get(list.size() - 1 - i));
            list.set(list.size() - 1 - i, s);
        }
        System.out.println(list);
        Collections.reverse(list);
        System.out.println(list);
        System.out.println(list.indexOf("java"));
        replaceAll(list,"java","adab");
        System.out.println(list);
    }
    public static void replaceAll(ArrayList<String> list,String oldValue,String newValue){
        for (int i = 0; i < list.size(); i++) {
            if (Objects.equals(list.get(i), oldValue)){
                list.set(i,newValue);
            }
        }
    }

}
